import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'counterpartyInformationM',
  state: {
    tableData: {}, //交易对手列表
  },
  effects: {
    // 删除交易对手
    *deleteCounterparty({ payload, callback }, { call, put, select }) {
      const res = yield call(api.counterpartyInformation.deleteCounterparty, payload);
      if (res) {
        if (res.code == 0) {
          message.success(res.msg || '删除成功！');
          callback('success');
        } else {
          message.error(res.msg || '删除交易对手失败！');
        }
      } else {
        message.error('删除交易对手失败！');
      }
    },
    // 查询交易对手列表
    *queryCounterpartyList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.counterpartyInformation.queryCounterpartyList, payload);
      if (res) {
        if (res.code == 0) {
          res.data.columns.unshift({ key: 'missing', title: '' });
          yield put({
            type: 'setState',
            payload: {
              tableData: res.data || {},
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              tableData: {},
            },
          });
          message.error(res.msg || '查询交易对手列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            tableData: {},
          },
        });
        message.error('查询交易对手列表失败!');
      }
    },
    // 新增或者编辑交易对手详情
    *saveOrUpdateBasic({ payload, callback }, { call, put, select }) {
      let res;
      if (payload.tempId) {
        res = yield call(api.counterpartyInformation.updateBasicProcess, payload);
      } else {
        res = yield call(api.counterpartyInformation.saveOrUpdateBasic, payload);
      }
      if (res) {
        if (res.code == 0) {
          message.success('保存成功!');

          callback();
        } else {
          message.error(res.msg || '保存失败!');
        }
      } else {
        message.error('保存失败!');
      }
    },
    // 获取详情页信息(编辑页初始化数据或详情页)
    *getBasicDetail({ payload, callback }, { call, put, select }) {
      let res;
      if (payload.approval) {
        res = yield call(api.counterpartyInformation.getBasicDetailProcess, payload);
      } else {
        res = yield call(api.counterpartyInformation.getBasicDetail, payload);
      }
      if (res) {
        if (res.code == 0) {
          res.data.counterparties.traders.dataSource &&
            res.data.counterparties.traders.dataSource.map((item, key) => {
              item.key = key;
            });
          res.data.counterparties.rate.dataSource &&
            res.data.counterparties.rate.dataSource.map((item, key) => {
              item.key = key;
            });
          res.data.organ.beneficiary.dataSource &&
            res.data.organ.beneficiary.dataSource.map((item, key) => {
              item.key = key;
            });
          res.data.organ.controllingShareholder.dataSource &&
            res.data.organ.controllingShareholder.dataSource.map((item, key) => {
              item.key = key;
            });
          res.data.organ.otherShareholder.dataSource &&
            res.data.organ.otherShareholder.dataSource.map((item, key) => {
              item.key = key;
            });
          res.data.organ.representative.dataSource &&
            res.data.organ.representative.dataSource.map((item, key) => {
              item.key = key;
            });
          callback({ params: res.data }, 'success');
        } else {
          callback({}, 'fail');
          message.error(res.msg || '查询交易对手详情失败!');
        }
      } else {
        callback({}, 'fail');
        message.error('查询交易对手详情失败!');
      }
    },
    // 获取母机构列表
    *getParentOrgList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.counterpartyInformation.getParentOrgList, payload);
      if (res) {
        const params = payload.id != 1 ? { parentOrgListSelect: res.data || [] } : { parentOrgList: res.data || [] };
        if (res.code == 0) {
          callback(params, 'success');
        } else {
          callback({}, 'fail');
          message.error(res.msg || '查询母机构列表失败!');
        }
      } else {
        callback({}, 'fail');
        message.error('查询母机构列表失败!');
      }
    },
    // 获取母机构列表
    *getQueryShortName({ payload, callback }, { call, put, select }) {
      const res = yield call(api.counterpartyInformation.queryShortNameLike, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res && res.msg ? res.msg : '查询数据失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
